package org.jftstool.auxiliar;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class InsertBlob {
	static String url = "jdbc:mysql://localhost/documentos";
	static String username = "root";
	static String password = "root";

	public static void main(String[] args) throws Exception {
	    Class.forName("org.gjt.mm.mysql.Driver");
	    Connection conn = DriverManager.getConnection(url, username, password);
	    conn.setAutoCommit(false);
	    FileInputStream fis = null;
	    File image = new File("C:\\Archivosprueba\\pdf\\documento.pdf");
	    fis = new FileInputStream(image);	    
    
	    int id = 1;
	    while(id < 10){
	    
		    String sql = "INSERT INTO documentos (id, binario) VALUES (?, ?)";
		    PreparedStatement stmt = conn.prepareStatement(sql);
		    stmt.setInt(1, id);
		
		    stmt.setBinaryStream(2, fis, (int) image.length());
		    stmt.execute();
		    conn.commit();
		    
		    fis = new FileInputStream(image);
		    
		    id++;
		    System.out.println(id);
	    }
	    fis.close();
	    conn.close();
	}
}
